package com.bjbn;

import com.bjbn.bean.cache.Cache;
import com.bjbn.bean.entity.BaseAirport;
import com.bjbn.bean.entity.BaseAirway;
import com.bjbn.bean.entity.BaseStand;
import com.bjbn.service.AirportService;
import com.bjbn.service.AirwayService;
import com.bjbn.service.StandService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class AppStartupRunner implements CommandLineRunner {

    private final AirportService airportService;

    private final AirwayService airwayService;

    private final StandService standService;

    public AppStartupRunner(AirportService airportService, AirwayService airwayService, StandService standService) {
        this.airportService = airportService;
        this.airwayService = airwayService;
        this.standService = standService;
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Application started - preloading data to cache...");


        List<BaseAirport> airportList = airportService.getAllAirport();
        for (BaseAirport airport : airportList) {
            Cache.baseAirportMap.put(airport.getCode(), airport);
        }

        List<BaseAirway> airwayList = airwayService.getAllAirway();
        for (BaseAirway airway : airwayList) {
            Cache.baseAirwayMap.put(airway.getCode(), airway);
        }

        List<BaseStand> standList = standService.getAllStand();
        for (BaseStand stand : standList) {
            Cache.baseStandMap.put(stand.getCode(), stand);
        }

        System.out.println("缓存成功");

        System.out.println(Cache.baseAirportMap);
    }
}